/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poker.geneticAlgorithm;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author Benjamin L. Brodie <blbrodie@gmail.com>
 */
public class PopulationOfWeights implements Population<Individual<Double>> {
    private final int SIZE;
    private List<Individual<Double>> individuals;
    private Random random = new Random();
    
    //Public methods
    public Individual<Double> getRandomIndividual(){
        
        //safe return - returns a copy
        return new WeightsIndividual(individuals.get(random.nextInt(SIZE)));
    }
    
    public void replaceRandomWith(Individual<Double> individual){
        
        individuals.remove(random.nextInt(SIZE)); //deletes a rnadom individual
        individuals.add(new WeightsIndividual(individual)); //add the guy - SAFE COPY
        
    }
    
    
    
    
    //Constructors
    public PopulationOfWeights(int size, int individualLenght){
        this.SIZE = size;
        this.individuals = generateRandomPopulation(size, individualLenght);
    }
    
    @Override
    public String toString(){
        String str = "Population:\n";
        
        str += "Size: " + SIZE + "\n";
        
        int count = 0;
        for (Individual<Double> individual : individuals){
            str += (count++) + ": " + individual + "\n";
        }
        
        return str;
        
    }
    
    
    
    
    private List<Individual<Double>> generateRandomPopulation(int size, int len){
        List<Individual<Double>> inds = new ArrayList<Individual<Double>>(size);
        
        for (int i = 0; i < size; i++){
            inds.add(new WeightsIndividual(len));
        }
        
        return inds;
    }
    
}
